package com.mariusz.service;

import java.util.List;

import javax.xml.ws.Holder;

import david.service.AwsHandlerResolver;
import awsClient4.AWSECommerceService;
import awsClient4.AWSECommerceServicePortType;
import awsClient4.Item;
import awsClient4.ItemSearch;
import awsClient4.ItemSearchRequest;
import awsClient4.Items;
import awsClient4.OperationRequest;
import awsClient4.SimilarityLookup;
import awsClient4.SimilarityLookupRequest;

public class test {
	private static String access_key = "AKIAINW4KL6V5T7HXHCQ";
	private static String secret_key = "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";
	private static AWSECommerceService service;
	private static AWSECommerceServicePortType port;

	static void connect(String new_access_key, String new_secret_key) {
		service = new AWSECommerceService();
		service.setHandlerResolver(new AwsHandlerResolver(secret_key));

		port = service.getAWSECommerceServicePort();

		access_key = new_access_key;
		secret_key = new_secret_key;
	}

	private static String formatItem(Item item) {

		StringBuilder sb = new StringBuilder();

		sb.append("[ASIN]: " + item.getASIN());
		sb.append(", [Name]: " + item.getItemAttributes().getTitle());
		sb.append(", [Manufacturer]: "
				+ ((item.getItemAttributes() != null && item
						.getItemAttributes().getManufacturer() != null) ? item
						.getItemAttributes().getManufacturer() : null));
		if (item.getOffers() != null
				&& item.getOffers().getTotalOffers().intValue() > 0) {
			sb.append(", [Price]: "
					+ item.getOffers().getOffer().get(0).getOfferListing()
							.get(0).getPrice().getFormattedPrice());
		}
		sb.append(", [Lowest new price]: "
				+ ((item.getOfferSummary() != null && item.getOfferSummary()
						.getLowestNewPrice() != null) ? item.getOfferSummary()
						.getLowestNewPrice().getFormattedPrice() : null));
		sb.append(", [Lowest used price]: "
				+ ((item.getOfferSummary() != null && item.getOfferSummary()
						.getLowestUsedPrice() != null) ? item.getOfferSummary()
						.getLowestUsedPrice().getFormattedPrice() : null));

		return sb.toString();
	}

	static void printAnswer(Items answer) {

		List<Item> item_list = answer.getItem();

		for (Item item : item_list) {
			System.out.println(formatItem(item));
			// for(int i=0;i<item.getBrowseNodes().getBrowseNode().size();i++)
			// {
			// ancestors(item.getBrowseNodes().getBrowseNode().get(i));
			// System.out.println();
			// }
			// System.out.print(item.getBrowseNodes().getBrowseNode().get(0).getName()+" ### ");
			// System.out.print(item.getBrowseNodes().getBrowseNode().get(0).getAncestors().getBrowseNode().get(0).getName()+" ### ");
			// System.out.println(item.getBrowseNodes().getBrowseNode().get(0).getAncestors().getBrowseNode().get(0).getAncestors().getBrowseNode().get(0).getName());
			// System.out.println();
			if (item.getSimilarProducts() != null) {
				for (int i = 0; i < item.getSimilarProducts()
						.getSimilarProduct().size(); i++)
					System.out.print(item.getSimilarProducts()
							.getSimilarProduct().get(i).getASIN()
							+ " ");
			} else
				System.out.print("no similar products");
			// System.out.println("\n" + item.getLargeImage().getURL());
			// System.out.println(item.getItemLinks().getItemLink().get(0)
			// .getURL());
			System.out.println("\n");

		}
	}

	public static void main(String args[]) throws Exception {

		connect("AKIAINW4KL6V5T7HXHCQ",
				"z/3NM0Rduv1fH2RVgRcJln6ok6eW6LpIVMXs/99b");

		ItemSearch search = new ItemSearch();
		ItemSearchRequest request = new ItemSearchRequest();
		request.setSearchIndex("All"); // ||Books||
		request.setKeywords("star wars");

		// request.getResponseGroup().add("MostGifted");
		search.getRequest().add(request);
		
		ItemSearchRequest request2 = new ItemSearchRequest();
		request2.setSearchIndex("All"); // ||Books||
		request2.setKeywords("enigma");

		// request.getResponseGroup().add("MostGifted");
		search.getRequest().add(request2);
		
		ItemSearchRequest request3 = new ItemSearchRequest();
		request3.setSearchIndex("All"); // ||Books||
		request3.setKeywords("enigma");
		search.getRequest().add(request3);

		
		search.setAWSAccessKeyId(access_key);
		search.setAssociateTag("");

		Holder<OperationRequest> operation_request = new Holder<OperationRequest>();
		Holder<List<Items>> items = new Holder<List<Items>>();

		port.itemSearch(
				search.getMarketplaceDomain(),
				search.getAWSAccessKeyId(),
				// search.getSubscriptionId(), I had to comment this out
				search.getAssociateTag(), search.getXMLEscaping(),
				search.getValidate(), search.getShared(), search.getRequest(),
				operation_request, items);

		
		
		
	/*	
		SimilarityLookupRequest requestSim = new SimilarityLookupRequest();
		SimilarityLookup searchSim = new SimilarityLookup();
		requestSim.getItemId().add("B007Q0T4FE");
		requestSim.getItemId().add("B00814M37U");
		requestSim.getItemId().add("B00814M37U");
		requestSim.setSimilarityType("Random");
		searchSim.getRequest().add(requestSim);

		searchSim.getRequest().add(requestSim);
		searchSim.setAWSAccessKeyId(access_key);
		searchSim.setAssociateTag("");
		port.similarityLookup(searchSim.getMarketplaceDomain(), searchSim.getAWSAccessKeyId(), searchSim.getAssociateTag(),
				searchSim.getValidate(), searchSim.getXMLEscaping(), searchSim.getShared(), searchSim.getRequest(), operation_request, items);
*/
		System.out.println("ANSWERS:" + items.value.size());
		System.out.println("ANSWERS:" + items.value.get(0).getItem().size());
		System.out.println(items.value.get(0).getItem().get(0).getASIN());
		Items retval = items.value.get(0);

	}
}
